<div id="header"><p>Your History</p></div>
<?php
	// Add ability to select a sensor
	include("sensor_select.php");

	// Store post to session
	if(isset($_POST['startDate'])) $_SESSION['startDate'] = $_POST['startDate'];
	if(isset($_POST['endDate'])) $_SESSION['endDate'] = $_POST['endDate'];
	if(isset($_POST['startTime'])) $_SESSION['startTime'] = $_POST['startTime'];
	if(isset($_POST['endTime'])) $_SESSION['endTime'] = $_POST['endTime'];

	// If no session, store curent time and date
	if(!isset($_SESSION['startDate'])) $_SESSION['startDate'] = get_current_date();
	if(!isset($_SESSION['endDate'])) $_SESSION['endDate'] = get_current_date();
	if(!isset($_SESSION['startTime'])) $_SESSION['startTime'] = get_current_time();
	if(!isset($_SESSION['endTime'])) $_SESSION['endTime'] = get_current_time();
?>
<!-- Begin Form -->
<form name="daterange" action="?p=timeforms&action=data" method="post">
<div id="subheader"><p>Start Time</p></div>
<label for="startDate">Date:</label>
<?php
	echo '<input type="text" size=25 id="startDateField" name="startDate" class="dateField" value="'.$_SESSION['startDate'].'">';
?>
<br/>
<label for="startTime">Time:</label>
<?php
	echo '<input type="text" size=25 id="startTime" name="startTime" value="'.$_SESSION['startTime'].'">';
?>
<div id="subheader"><p>End Time</p></div>
<label for="endDate">Date:</label>
<?php
	echo '<input type="text" size=25 id="endDateField" name="endDate" class="dateField" value="'.$_SESSION['endDate'].'">';
?>
<br/>
<label for="endTime">Time:</label>
<?php
	echo '<input type="text" size=25 id="endTime" name="endTime" value="'.$_SESSION['endTime'].'">';
?>
<p class="submit"><input type="submit" value="Fetch" /></p>	
</form>
<!-- End Form -->
<br/>
<?php

	// Create timestamp from date and time
	$startTimestamp = date('Y-m-d H:i:s',strtotime($_SESSION['startDate'].' '.$_SESSION['startTime']));
	$endTimestamp = date('Y-m-d H:i:s', strtotime($_SESSION['endDate'].' '.$_SESSION['endTime']));	

	// If the start and end times are the same, don't bother with the rest
	if($startTimestamp == $endTimestamp)
	{
	}
	// If the end time is earlier than the start time, then produce an error
	else if($endTimestamp < $startTimestamp)
	{
		echo '<p>The selected End Time is earlier than the Start Time</p>';
	}
	else
	{
		echo '<div id="subheader"><p>Your Data</p></div>';
		
		echo '<p>A history of your room from <b>'.$startTimestamp.'</b> to <b>'.$endTimestamp.'</b>.</p>';

		// Present average temperature
		echo '<div id="currentdata">Average temperature: ';
		echo '<div id="temperature">';

		// Call the average temperature procedure
		$results = $database->multi_query("CALL getRawAggTemp(".$_SESSION['sensorID'].",\"$startTimestamp\",\"$endTimestamp\")");
		if($results)
		{
			$results = $database->use_result();
			while($row = $results->fetch_row())
			{
				echo '<div>'.round($row[0],1).'&#176</div>';
			}
			$results->free();
			if($database->more_results())
					while($database->next_result());
		}	
		echo '</div></div>';

		// The following produces a table of all of the historical data requested
		echo '<div>';

		// Get the data via getTimeRangeData procedure
		$results = $database->multi_query("CALL getTimeRangeData(".$_SESSION['sensorID'].",\"$startTimestamp\",\"$endTimestamp\")");
		if($results)
		{
			$results = $database->use_result();
			echo '<table>';
			echo '<tr><th>Date</th><th>Time</th><th>Temperature</th><th>Light</th><th>Door</th></tr>';
			while($row = $results->fetch_row())
			{
				echo '<tr>';
				echo '<td>'.date("Y-m-d",strtotime($row[2])).'</td>';
				echo '<td>'.date("g:i:s A",strtotime($row[2])).'</td>';
				echo '<td>'.$row[3].'</td>';
				echo '<td>'.$row[4].'</td>';
				echo '<td>'.$row[5].'</td>';	
				echo '</tr>';
			}
			echo '</table>';
		}
		else
		{
			echo '<p>There was a database error:'.$database->error.'</p>';
		}
		echo '</div>';
	}
?>

<!-- The following sets up the calendaryview and clickPicker -->

<link rel="stylesheet" href="./javascript/calendarview/stylesheets/calendarview.css">
<link rel="stylesheet" href="./javascript/clockPicker.css" type="text/css">
<script src="./javascript/calendarview/javascripts/prototype.js"></script>
<script type="text/javascript" src="./javascript/jquery.js"></script>
<script src="./javascript/calendarview/javascripts/calendarview.js"></script>
<script type="text/javascript" src="./javascript/clockPicker.js"></script>
<!-- Setup ClockPicker -->
<script type="text/javascript">
var $j = jQuery.noConflict();

$j(document).ready(function()
{
	$j("#startTime").clockpick({
		starthour:0,
		endhour:23,
		showminutes:false
	});
	$j("#endTime").clockpick({
		starthour:0,
		endhour:23,
		showminutes:false
	});
});
</script>
<!-- Setup Calendars -->
<script>
	function setupCalendars() {
		Calendar.setup(
			{
				dateField: 'startDateField',
				triggerElement: 'startDateField'
			}
		)
		Calendar.setup(
			{
				dateField: 'endDateField',
				triggerElement: 'endDateField'
			}
		)
	}
  Event.observe(window, 'load', function() { setupCalendars() })
</script>
